#include "form_setting_mode.h"
#include "ui_form_setting_mode.h"

// add necessary includes here
#include "utils/configure/configure_bll.h"
#include "ui/setting/form_mode_verify_dlg.h"

static QString gs_sModeNameOld = "";


/*
 * Function:    FormSettingMode
 * Description: The default constructor
 * Paramer:     void
 * Return:
 */
FormSettingMode::FormSettingMode(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::FormSettingMode)
{
    qDebug() << Q_FUNC_INFO;

    ui->setupUi(this);
}

/*
 * FormHistoryResult:    ~FormSettingMode
 * Description: The default destructor
 * Paramer:     void
 * Return:
 */
FormSettingMode::~FormSettingMode()
{
    qDebug() << Q_FUNC_INFO;

    delete ui;
}

/*
 * Function:    getInstance
 * Description: Get an instance of "FormSettingMode" class, if exist, return it
 * Paramer:     QWidget *pParent - the pointer of QWidget class
 * Return:      the pointer of "FormSettingMode" class
 */
FormSettingMode* FormSettingMode::getInstance(QWidget *pParent)
{
    static FormSettingMode *pNew = NULL;
    if(!pNew) {
        qDebug() << Q_FUNC_INFO;

        // Create a new object if it doesn't exist
        pNew = new FormSettingMode(pParent);
        if(NULL == pNew) return NULL;

        // After constructing object, initialize the object
        pNew->_Initialze();

        // Initialize control's UI
        pNew->_InitUi();

        // Hide widget firstly
        pNew->hide();
    }
    else {
        // Set the parent object
        pNew->setParent(pParent);
    }

    return pNew;
}

/*
 * Function:    _Initialze
 * Description: After constructing object, initialize the object
 * Paramer:     void
 * Return:      void
 */
void FormSettingMode::_Initialze()
{
    qDebug() << Q_FUNC_INFO;
    std::vector<bool> vecConnect;
    vecConnect.push_back(QObject::connect(this, SIGNAL(signalRefreshUI()), this, SLOT(slotRefreshUI()), static_cast<Qt::ConnectionType>(Qt::AutoConnection | Qt::UniqueConnection)));
    for(int i=0; i<(int)vecConnect.size(); i++) {
        Q_ASSERT(vecConnect.at(i));
    }
}

/*
* Function:		_InitUi
* Description:	Init the window style
* Paramer:		void
* Return:		void
*/
void FormSettingMode::_InitUi()
{
    qDebug() << Q_FUNC_INFO;

    // Displays the background image of the window
    QWidget::setAttribute(Qt::WA_StyledBackground);

    SET_STYLE_SHEET(QString(":/%1/setting/%2.qss").arg("skin_1").arg(this->objectName()));

    ui->pushButton_UserMode->setFocusPolicy(Qt::NoFocus);
    ui->pushButton_AdminMode->setFocusPolicy(Qt::NoFocus);
    ui->pushButton_FactoryMode->setFocusPolicy(Qt::NoFocus);

    ui->pushButton_UserMode->setCheckable(true);
    ui->pushButton_AdminMode->setCheckable(true);
    ui->pushButton_FactoryMode->setCheckable(true);
}

void FormSettingMode::on_pushButton_UserMode_clicked()
{
    qDebug() << Q_FUNC_INFO;

    if(tr("User Mode") == gs_sModeNameOld) {
        ui->pushButton_UserMode->setChecked(true);
        return;
    }

    ui->pushButton_UserMode->setChecked(true);
    ui->pushButton_AdminMode->setChecked(false);
    ui->pushButton_FactoryMode->setChecked(false);

    gs_sModeNameOld = tr("User Mode");

    // Save the configure
    _saveModeConfig();
}

void FormSettingMode::on_pushButton_AdminMode_clicked()
{
    qDebug() << Q_FUNC_INFO;

    if(tr("Admin Mode") == gs_sModeNameOld) {
        ui->pushButton_AdminMode->setChecked(true);
        return;
    }

    ui->pushButton_UserMode->setChecked(false);
    ui->pushButton_FactoryMode->setChecked(false);

    // Display the mode verify dialog
    FormModeVerifyWidget::getInstance(this)->showModeVerifyDialog(ui->pushButton_AdminMode->text().trimmed(), gs_sModeNameOld);
}

void FormSettingMode::on_pushButton_FactoryMode_clicked()
{
    qDebug() << Q_FUNC_INFO;

    if(tr("Factory Mode") == gs_sModeNameOld) {
        ui->pushButton_FactoryMode->setChecked(true);
        return;
    }

    ui->pushButton_UserMode->setChecked(false);
    ui->pushButton_AdminMode->setChecked(false);

    // Display the mode verify dialog
    FormModeVerifyWidget::getInstance(this)->showModeVerifyDialog(ui->pushButton_FactoryMode->text().trimmed(), gs_sModeNameOld);
}

/*
* Function:		slotRefreshUI
* Description:	The slot that refresh UI info
* Paramer:		void
* Return:		void
*/
void FormSettingMode::slotRefreshUI()
{
    qDebug() << Q_FUNC_INFO;

    // Get the login mode
    ST_CFG_LOGIN_MODE stLoginMode;

    ConfigureBLL::getInstance(this)->loadLoginMode(stLoginMode);

    if(EUM_LOGIN_MODE::E_MODE_GUEST == stLoginMode.eumLoginMode) {
        gs_sModeNameOld = tr("User Mode");

        ui->pushButton_UserMode->setChecked(true);
        ui->pushButton_AdminMode->setChecked(false);
        ui->pushButton_FactoryMode->setChecked(false);
    }
    else if(EUM_LOGIN_MODE::E_MODE_ADMIN == stLoginMode.eumLoginMode) {
        gs_sModeNameOld = tr("Admin Mode");

        ui->pushButton_UserMode->setChecked(false);
        ui->pushButton_AdminMode->setChecked(true);
        ui->pushButton_FactoryMode->setChecked(false);
    }
    else {
        gs_sModeNameOld = tr("Factory Mode");

        ui->pushButton_UserMode->setChecked(false);
        ui->pushButton_AdminMode->setChecked(false);
        ui->pushButton_FactoryMode->setChecked(true);
    }

    // Emit an signal that refresh login mode on the status bar
    emit signalRefreshLoginMode(stLoginMode.eumLoginMode);
}

/*
* Function:		slotRespondInfoFromDlg
* Description:	Get the reply message from the child window (search wifi)
* Paramer:		nt nReplyType - the reply type
*               bool bAccepted - is confirmed?
* Return:		void
*/
void FormSettingMode::slotRespondInfoFromDlg(int nReplyType, bool bAccepted)
{
    qDebug() << Q_FUNC_INFO << nReplyType << bAccepted;

    if(bAccepted) {
        if(E_MODE_ADMIN == nReplyType) {
            ui->pushButton_UserMode->setChecked(false);
            ui->pushButton_AdminMode->setChecked(true);
            ui->pushButton_FactoryMode->setChecked(false);

            gs_sModeNameOld = tr("Admin Mode");
        }
        else if(E_MODE_FACTORY == nReplyType) {
            ui->pushButton_UserMode->setChecked(false);
            ui->pushButton_AdminMode->setChecked(false);
            ui->pushButton_FactoryMode->setChecked(true);

            gs_sModeNameOld = tr("Factory Mode");
        }
        else {
            gs_sModeNameOld = tr("User Mode");
        }

        // Save the configure
        _saveModeConfig();
    }
    else {
        if(E_MODE_ADMIN == nReplyType) {
            ui->pushButton_UserMode->setChecked(false);
            ui->pushButton_AdminMode->setChecked(true);
            ui->pushButton_FactoryMode->setChecked(false);
        }
        else if(E_MODE_FACTORY == nReplyType) {
            ui->pushButton_UserMode->setChecked(false);
            ui->pushButton_AdminMode->setChecked(false);
            ui->pushButton_FactoryMode->setChecked(true);
        }
        else {
            ui->pushButton_UserMode->setChecked(true);
            ui->pushButton_AdminMode->setChecked(false);
            ui->pushButton_FactoryMode->setChecked(false);
        }
    }
}

/*
* Function:		changeEvent
* Description:	Triggers an event before it is switch language
* Paramer:		QShowEvent *event - the pointer of "QEvent"
* Return:		void
*/
void FormSettingMode::changeEvent(QEvent *e)
{
    switch (e->type()) {
    case QEvent::LanguageChange:
    {
        ui->retranslateUi(this);
        qDebug() << Q_FUNC_INFO;
        break;
    }
    default:
        break;
    }

    QWidget::changeEvent(e);
}

/*
* Function:		_SaveModeConfig
* Description:	Save the configure
* Paramer:		void
* Return:		void
*/
void FormSettingMode::_saveModeConfig()
{
    qDebug() << Q_FUNC_INFO;

    ST_CFG_LOGIN_MODE stLoginMode;

    stLoginMode.eumLoginMode = EUM_LOGIN_MODE::E_MODE_GUEST;
    if(ui->pushButton_AdminMode->isChecked()) {
        stLoginMode.eumLoginMode = EUM_LOGIN_MODE::E_MODE_ADMIN;
    }
    else if(ui->pushButton_FactoryMode->isChecked()) {
        stLoginMode.eumLoginMode = EUM_LOGIN_MODE::E_MODE_FACTORY;
    }

    // Set the login mode
    ConfigureBLL::getInstance(this)->saveLoginMode(stLoginMode);

    // Emit an signal that refresh login mode on the status bar
    emit signalRefreshLoginMode(stLoginMode.eumLoginMode);
}
